/**
 * 
 */
package sailing.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import cjlite.logger.Logger;

import sailing.filters.Filter;
import sailing.filters.FilterListModel;

/**
 * @author kevin
 * 
 */
public class FilterMenuItemAction extends AbstractAction {

	private static Logger logger = Logger.thisClass();

	private final Filter filter;
	private FilterListModel filterListModel;

	private Class<? extends Filter> filterClass;

	public FilterMenuItemAction(Filter filter, FilterListModel filterListModel) {
		super(filter.getName());
		this.filter = filter;
		this.filterListModel = filterListModel;
	}

	public FilterMenuItemAction(Class<? extends Filter> filter2, FilterListModel filterListModel2) {
		this.filter = this.getFilterInstance(filter2);
		this.filterListModel = filterListModel2;
		putValue(Action.NAME, this.filter.getName());
	}

	private Filter getFilterInstance(Class<? extends Filter> filter2) {
		try {
			return filter2.newInstance();
		} catch (Exception e) {
			logger.error("Horrible, can not instantiate filter Class{0}", e, filter2.getName());
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		logger.trace("MenuItem clicked: {0}", filter.getName());
		if (filterListModel != null) {
			if (this.filter != null) {
				filterListModel.addFilter(this.filter);
			} else {
				logger.trace("filter is null, that should be a bug issue (menuitem={0})", filterClass.getName());
			}

		} else {
			logger.trace("filterListModel is null, that should be a bug issue (menuitem={0})", filter.getName());
		}
	}
}
